﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TicketReservationSystemDataLayer
{
    public class LocationDAO
    {
        public static IQueryable<Location> GetAllLocations(
            TicketReservationSystemDataContext context)
        {
            var locations =
                from l in context.Locations
                select l;
            return locations;
        }

        public static bool DeleteLocation(
            TicketReservationSystemDataContext context, Location location)
        {
            try
            {
                context.Locations.DeleteOnSubmit(location);
                context.SubmitChanges();
                return true;
            }
            catch(Exception)
            {
                return false;
            }
        }

        public static bool DeleteLocationById(
            TicketReservationSystemDataContext context, int locationId)
        {
            Location location = FindLocationById(context, locationId);
            return DeleteLocation(context, location);
        }

        public static Location FindLocationById(
            TicketReservationSystemDataContext context, int locationId)
        {
            var location =
                from c in context.Locations
                where c.Id == locationId
                select c;
            return location.FirstOrDefault();
        }

        public static bool CreateLocation(
            TicketReservationSystemDataContext context, string locationName)
        {
            try
            {
                Location Location = new Location();
                Location.Name = locationName;
                context.Locations.InsertOnSubmit(Location);
                context.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool EditLocation(
            TicketReservationSystemDataContext context, Location Location, string newName)
        {
            try
            {
                Location.Name = newName;
                context.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
